import config from "../config"
const BASEURL = 'https://love.qiuhuiyi.cn/api/wechat'

class Service {
    api(params) {
        return new Promise((resolve, reject) => {
            params.data.app_code = config.alianceKey
            uni.showLoading({
                title: '正在加载'
            })
            let token = uni.getStorageSync('token');
            uni.request({
                url: BASEURL + params.url,
                method: params.method,
                header: {
                    'content-type': 'application/json',
                    'Authorization': 'Bearer ' + token
                },
                data: params.data,
                success: (res) => {
                    resolve(res.data)
                },
                fail: (error) => {
                    reject(error)
                },
                complete: () => {
                    uni.hideLoading()
                }
            })
        })
    }
    get(params) {
        params.method = 'GET'
        return this.api(params)
    }
    post(params) {
        params.method = 'POST'
        return this.api(params)
    }
    put(params) {
        params.method = 'PUT'
        return this.api(params)
    }
    delete(params) {
        params.method = 'DELETE'
        return this.api(params)
    }
}

export default Service